import { myRequest } from '../../api/myRequest'
import { actionsMap } from '../../api/url'
import lodash from 'lodash'

const Configuration = (actionName) => {
  let $action = actionsMap[actionName]
  return {
    // 查询
    queryAll() {
      return myRequest(actionName, [{ action: $action.MODEL_QUERY_ALL }])
    },
    queryInfoByPage(pageNumber, pageSize, filter) {
      return myRequest(actionName, [
        { action: $action.MODEL_QUERY_INFO_BY_PAGE, pageNumber: pageNumber, pageSize: pageSize, ...filter },
      ])
    },
    queryVersionInfoByPage(pageNumber, pageSize, filter) {
      return myRequest(actionName, [
        { action: $action.MODEL_QUERY_VERSION_INFO_BY_PAGE, pageNumber: pageNumber, pageSize: pageSize, ...filter },
      ])
    },
    query(filter) {
      return myRequest(actionName, [{ action: $action.MODEL_QUERY, ...filter }])
    },
    add(record) {
      return myRequest(actionName, [{ action: $action.MODEL_ADD, data: lodash.isArray(record) ? record : [record] }])
    },
    updateById(record) {
      return myRequest(actionName, [{ action: $action.MODEL_UPDATE_BY_ID, data: [record] }])
    },
    deleteByClassNames(classNames) {
      return myRequest(actionName, [{ action: $action.MODEL_DELETE_BY_CLASSNAMES, classNames }])
    },
    deleteByIds(ids) {
      return myRequest(actionName, [{ action: $action.MODEL_DELETE_BY_IDS, ids }])
    },
    upgrade(className, version) {
      return myRequest(actionName, [{ action: $action.MODEL_UPGRADE, className: className, version: version }])
    },
    setPublish(className, version) {
      return myRequest(actionName, [
        { action: $action.MODEL_SET_PUBLISH, className: className, version: version },
      ])
    },
    setUnpublish(className, version) {
      return myRequest(actionName, [
        { action: $action.MODEL_SET_UN_PUBLISH, className: className, version: version },
      ])
    },
    updateHierProperties(record) {
      return myRequest(actionName, [{ action: $action.MODEL_UPDATE_HIER_PROPERTIES, data: [record] }])
    },
  }
}

const Instance = (actionName) => {
  let $action = actionsMap[actionName]
  return {
    /**
     * 查询列表
     * @param recipient
     * @returns {Promise<*>}
     */
    queryInfoByPage(pageNumber, pageSize, filter) {
      let recipient = [
        { action: $action.INSTANCE_QUERY_INFO_BY_PAGE, pageNumber: pageNumber, pageSize: pageSize, ...filter },
      ]
      return myRequest(actionName, recipient)
    },
    /**
     * 分页查询
     * @param pageNumber
     * @param pageSize
     * @param filter
     * @returns {Promise<unknown | void>}
     */
    queryByPage(pageNumber, pageSize, filter) {
      let recipient = [{action: $action.INSTANCE_QUERY_BY_PAGE, pageNumber: pageNumber, pageSize: pageSize,...filter}]
      return myRequest(actionName, recipient);
    },
    /**
     * 查询列表
     * @param recipient
     * @returns {Promise<*>}
     */
    query(filter) {
      let recipient = [{ action: $action.INSTANCE_QUERY, ...filter }]
      return myRequest(actionName, recipient)
    },
    queryById(id) {
      let recipient = [{ action: $action.INSTANCE_QUERY, condition:`id=${id}` }]
      return myRequest(actionName, recipient)
    },
    queryByIds(ids) {
      let recipient = [{ action: $action.INSTANCE_QUERY_BY_IDS,ids }]
      return myRequest(actionName, recipient)
    },

    /**
     * 添加信息
     * @param record
     * @returns {Promise<*>}
     */
    add(record) {
      return myRequest(actionName,[{action: $action.INSTANCE_ADD, data: [record] }]);
    },
    addAndInitWeb(record) {
      return myRequest(actionName,[{action: $action.INSTANCE_ADD_AND_INIT_WEB, data: [record] }]);
    },
    /**
     * 批量添加信息
     * @param records
     * @returns {Promise<*>}
     */
    batchAdd(records) {
      return myRequest(actionName,[{action: $action.INSTANCE_BATCH_ADD, data: records}]);
    },
    /**
     * 更新信息
     * @param record
     * @returns {Promise<*>}
     */
    updateById(record, params={}) {
      return myRequest(actionName, [
        {
          action: $action.INSTANCE_UPDATE_BY_ID, data: [record], ...params
        }
      ]);
    },
    /**
     * 更新信息
     * @param record
     * @returns {Promise<*>}
     */
    updateByIds(records=[]) {
      return myRequest(actionName,[{action: $action.INSTANCE_UPDATE_BY_IDS, data: records}]);
    },
    /**
     * 批量删除信息
     * @param classNames
     * @returns {Promise<*>}
     */
    deleteByClassNames(classNames) {
      return myRequest(actionName, [{ action: $action.INSTANCE_REPEAL_BY_IDS, classNames }])
    },
    deleteByIds(ids) {
      // return myRequest(actionName, [{ action: $action.INSTANCE_REPEAL_BY_IDS, ids }])
      return myRequest(actionName, [{ action: $action.INSTANCE_DELETE_BY_IDS, ids }])
    },
    deleteByCondition(obj) {
      let recipient = [{ action: $action.INSTANCE_DELETE_BY_CONDITION, ...obj }]
      return myRequest(actionName, recipient)
    },
    queryAllByParentNodeId(params) {
      let recipient = [
        { action: $action.INSTANCE_QUERY_ALL_BY_PARENT_NODE_ID, ...params },
      ]
      return myRequest(actionName, recipient)
    },
    updateHier(record) {
      return myRequest(actionName, [{ action: $action.INSTANCE_UPDATE_HIER, data: [record] }])
    },
  }
}

export default {
  Configuration,
  Instance
}